home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-23 | 7.2 KB | 236 lines | [TEXT/PJMM] |
- unit MyProcesses;
-
- interface
-
- uses
- Processes;
-
- const
- application = 'APPL';
-
- function FindApplication (creator: OSType; var fs: FSSpec): OSErr;
- function FindProcess (creator, typ: OSType; var process: ProcessSerialNumber; var fs: FSSpec): boolean;
- procedure LaunchWithDocument (creator, typ: OSType; fs: FSSpec; tofront: boolean);
- procedure LaunchApp (creator, typ: OSType; tofront: boolean);
- procedure QuitApplication (creator, typ: OSType);
- procedure LaunchFSSpec (var fs: FSSpec; tofront: boolean);
-
- implementation
-
- uses
- AppleEvents, Aliases, MySystemGlobals, MyUtils;
-
- function FindApplication (creator: OSType; var fs: FSSpec): OSErr;
- var
- i: integer;
- pbdt: DTPBRec;
- crdate: longInt;
- oe: OSErr;
- found: boolean;
- begin
- found := false;
- if system7 then begin
- i := 1;
- repeat
- fs.vRefNum := 0;
- oe := GetVolInfo(fs.name, fs.vRefNum, i, crdate);
- i := i + 1;
- if oe = noErr then begin
- with pbdt do begin
- fs.name := '';
- ioNamePtr := @fs.name;
- ioVRefNum := fs.vRefNum;
- oe := PBDTGetPath(@pbdt);
- if oe = noErr then begin
- ioIndex := 0;
- ioFileCreator := creator;
- oe := PBDTGetAPPLSync(@pbdt);
- if oe = noErr then
- found := true;
- end;
- end;
- oe := noErr;
- end;
- until found or (oe <> noErr);
- end;
- if found then begin
- oe := noErr;
- fs.parID := pbdt.ioAPPLParID;
- end
- else begin
- oe := afpItemNotFound;
- fs.vRefNum := 0;
- fs.parID := 2;
- fs.name := '';
- end;
- FindApplication := oe;
- end;
-
- function FindProcess (creator, typ: OSType; var process: ProcessSerialNumber; var fs: FSSpec): boolean;
- var
- info: ProcessInfoRec;
- oe: OSErr;
- gv: longInt;
- begin
- FindProcess := false;
- if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) then begin
- process.highLongOfPSN := 0;
- process.lowLongOfPSN := kNoProcess;
- info.processInfoLength := sizeof(ProcessInfoRec);
- info.processName := nil;
- info.processAppSpec := @fs;
- while GetNextProcess(process) = noErr do begin
- if GetProcessInformation(process, info) = noErr then begin
- if (info.processType = longInt(typ)) and (info.processSignature = creator) then begin
- FindProcess := true;
- leave;
- end;
- end;
- end;
- end;
- end;
-
- procedure AddFSSToAEList (var list: AEDescList; row: integer; var fs: FSSpec);
- var
- fileAlias: AliasHandle;
- err: OSErr;
- begin
- err := NewAlias(nil, fs, fileAlias);
- if err = noErr then begin
- HLock(handle(fileAlias));
- err := AEPutPtr(list, row, typeAlias, ptr(fileAlias^), fileAlias^^.aliasSize);
- DisposHandle(handle(fileAlias));
- end;
- end;
-
- procedure PrepareToLaunch (var theEvent: AppleEvent; tofront: boolean; var launchDesc: AEDesc; var launchThis: LaunchParamBlockRec);
- var
- oe: OSErr;
- begin
- oe := AECoerceDesc(theEvent, typeAppParameters, launchDesc);
- HLock(handle(theEvent.dataHandle));
- launchThis.launchAppParameters := AppParametersPtr(launchDesc.dataHandle^);
- launchThis.launchBlockID := extendedBlock;
- launchThis.launchEPBLength := extendedBlockLen;
- launchThis.launchFileFlags := 0;
- launchThis.launchControlFlags := launchContinue + launchNoFileFlags;
- if not tofront then
- launchThis.launchControlFlags := launchThis.launchControlFlags + launchDontSwitch;
- end;
-
- procedure LaunchWithDocument (creator, typ: OSType; fs: FSSpec; tofront: boolean);
- var
- psn: ProcessSerialNumber;
- targetAddress: AEDesc;
- theEvent, theReply: AppleEvent;
- fileList: AEDescList;
- launchDesc: AEDesc;
- app_fs: FSSpec;
- launchThis: LaunchParamBlockRec;
- oe: OSErr;
- gv: longInt;
- sendmode: AESendMode;
- t, c: longInt;
- begin
- PurgeSpace(t, c);
- if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) & (c > 4096) then begin
- if FindProcess(creator, typ, psn, app_fs) then begin
- oe := AECreateDesc(typeProcessSerialNumber, @psn, sizeof(psn), targetAddress);
- oe := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
- oe := AEDisposeDesc(targetAddress);
- oe := AECreateList(nil, 0, false, fileList);
- AddFSSToAEList(fileList, 1, fs);
- oe := AEPutParamDesc(theEvent, keyDirectObject, fileList);
- sendmode := kAENoReply;
- if not tofront then
- sendmode := sendmode + kAENeverInteract;
- oe := AESend(theEvent, theReply, sendmode, kAEHighPriority, kNoTimeOut, nil, nil);
- oe := AEDisposeDesc(theEvent);
- oe := AEDisposeDesc(theReply);
- oe := AEDisposeDesc(fileList);
- if tofront then
- oe := SetFrontProcess(psn);
- end
- else begin
- if FindApplication(creator, app_fs) = noErr then begin
- oe := AECreateDesc(typeApplSignature, @creator, sizeof(creator), targetAddress);
- oe := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
- oe := AEDisposeDesc(targetAddress);
- oe := AECreateList(nil, 0, false, fileList);
- AddFSSToAEList(fileList, 1, fs);
- oe := AEPutParamDesc(theEvent, keyDirectObject, fileList);
- launchThis.launchAppSpec := @app_fs;
- PrepareToLaunch(theEvent, tofront, launchDesc, launchThis);
- oe := LaunchApplication(@launchThis);
- oe := AEDisposeDesc(theEvent);
- oe := AEDisposeDesc(fileList);
- end;
- end;
- end;
- end;
-
- procedure LaunchFSSpec (var fs: FSSpec; tofront: boolean);
- var
- oe: OSErr;
- fi: FInfo;
- targetAddress: AEDesc;
- theEvent: AppleEvent;
- gv: longInt;
- launchThis: LaunchParamBlockRec;
- launchDesc: AEDesc;
- begin
- if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) then begin
- oe := FSpGetFInfo(fs, fi);
- oe := AECreateDesc(typeApplSignature, @fi.fdCreator, sizeof(fi.fdCreator), targetAddress);
- oe := AECreateAppleEvent(kCoreEventClass, kAEOpenApplication, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
- oe := AEDisposeDesc(targetAddress);
- launchThis.launchAppSpec := @fs;
- PrepareToLaunch(theEvent, tofront, launchDesc, launchThis);
- oe := LaunchApplication(@launchThis);
- oe := AEDisposeDesc(theEvent);
- end;
- end;
-
- procedure LaunchApp (creator, typ: OSType; tofront: boolean);
- var
- psn: ProcessSerialNumber;
- fileList: AEDescList;
- app_fs: FSSpec;
- oe: OSErr;
- gv: longInt;
- sendmode: AESendMode;
- begin
- if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) then begin
- if FindProcess(creator, typ, psn, app_fs) then begin
- if tofront then begin
- oe := SetFrontProcess(psn);
- end;
- end
- else begin
- if FindApplication(creator, app_fs) = noErr then begin
- LaunchFSSpec(app_fs, tofront);
- end;
- end;
- end;
- end;
-
- procedure QuitApplication (creator, typ: OSType);
- var
- process: processSerialNumber;
- infoRec: processInfoRec;
- targetAddress: AEAddressDesc;
- AEvent, AReply: AppleEvent;
- fs: FSSpec;
- oe: OSErr;
- begin
- if FindProcess(creator, typ, process, fs) then begin
- oe := AECreateDesc(typeProcessSerialNumber, @process, SizeOf(process), targetAddress);
- oe := AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, AEvent);
- oe := AEDisposeDesc(targetAddress);
- oe := AESend(AEvent, AReply, kAENoReply, kAEHighPriority, 5 * 60, nil, nil);
- oe := AEDisposeDesc(AEvent);
- oe := AEDisposeDesc(AReply);
- end;
- end;
-
- end.